package com.biz.primus.ms.sms.service.checker;

import com.biz.primus.model.misc.enums.ApiExceptionType;
import com.biz.primus.model.misc.exception.ApiException;
import com.biz.primus.model.misc.vo.ChannelBasedAuth;
import com.biz.primus.model.misc.vo.MultiReceiverShortMessageVo;
import com.biz.primus.model.misc.vo.ShortMessageVo;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * @author yanweijin
 * @date 2017/5/31
 */
@Component
public class MobileChecker extends SendCheckerAdapter {


	private Pattern pattern = Pattern.compile("^[1]+[3|4|5|7|8][0-9]{9}$");

	@Override
	public void check(ChannelBasedAuth cba) throws ApiException {

		if(cba instanceof ShortMessageVo){
			if(!pattern.matcher(((ShortMessageVo) cba).getMobile()).find()){
				throw new ApiException(ApiExceptionType.MOBILE_ERROR);
			}
		}else if(cba instanceof MultiReceiverShortMessageVo){
			for (String mobile : ((MultiReceiverShortMessageVo) cba).getMobiles()) {
				if(!pattern.matcher(mobile).find()){
					throw new ApiException(ApiExceptionType.MOBILE_ERROR);
				}
			}
		}
	}

	@Override
	public int getOrder() {
		return 34;
	}
}
